package com.igoal.web;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.CityDTO;
import com.igoal.dto.CompanyDTO;
import com.igoal.dto.SchoolDTO;
import com.igoal.dto.UserInfoDTO;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserAccountService;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;

@Controller
public class AjaxUserAccountSettingsController extends BaseAjaxController {
	private UserAccountService userAccountService;
	
	@Autowired
	public void setUserAccountService(UserAccountService userAccountService) {
		this.userAccountService = userAccountService;
	}

	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager) {
		this.userGoalManager = userGoalManager;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}
	
	/*
	 * 'edit basic information'
	 * 'edit contact information'
	 * 'edit schools & company'
	 * 'edit resident'
	 * 'edit hobbies'
	 */
	@RequestMapping(method=RequestMethod.GET, value="/ajax/userAccountSettingsForm.html")
	public String showForm(ModelMap modelMap, @RequestParam("type") final int type){		
		String username = getUserInSession();
		UserInfoDTO userInfo = null;
		List<CityDTO> cities = userAccountService.getAllCities();		
		switch(type){
		case 1:
			userInfo = userAccountService.getUserInfo(username);
			modelMap.addAttribute("type", 1);
			modelMap.addAttribute("cities", cities);
			applyModel(modelMap, userInfo);
			break;
		case 2:
			userInfo = userAccountService.getUserInfo(username);
			modelMap.addAttribute("type", 2);
			applyModel(modelMap, userInfo);
			break;
		case 3:
			List<SchoolDTO> schools = userAccountService.getUserSchools(username);
			List<CompanyDTO> companies = userAccountService.getUserCompanies(username);
			modelMap.addAttribute("schools", schools);
			modelMap.addAttribute("companies", companies);
			modelMap.addAttribute("cities", cities);
			modelMap.addAttribute("type", 3);
			break;
		case 4:
			userInfo = userAccountService.getUserInfo(username);
			modelMap.addAttribute("type", 4);
			modelMap.addAttribute("cities", cities);
			applyModel(modelMap, userInfo);
			break;
		case 5:
			Map<String, Map<String, String>> hobbies = userAccountService.getUserHobbies(username);
			modelMap.addAttribute("type", 5);
			Set<String> keys = hobbies.keySet();
			for(String key : keys){
				Map<String, String> valueMap = hobbies.get(key);
				String value = "";
				Iterator<String> subKeys = valueMap.keySet().iterator();
				while(subKeys.hasNext()){
					String subKey = subKeys.next();
					value += valueMap.get(subKey);
					if(subKeys.hasNext()){
						value += ",";
					}
				}
				modelMap.addAttribute(key, value);
			}
			break;
		case 6:
			modelMap.addAttribute("type", 6);
			break;
		}
		
		return getViewPrefix() + "ajaxUserAccountSettingsForm";
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/ajax/userAccountSettingsPart.html")
	public String showPart(ModelMap modelMap, @RequestParam("type") final int type){		
		String username = getUserInSession();
		UserInfoDTO userInfo = null;
		List<CityDTO> cities = userAccountService.getAllCities();
		List<SchoolDTO> schools = null;
		List<CompanyDTO> companies = null;
		switch(type){
		case 1:
			userInfo = userAccountService.getUserInfo(username);
			modelMap.addAttribute("type", 1);
			modelMap.addAttribute("cities", cities);
			applyModel(modelMap, userInfo);
			break;
		case 2:
			userInfo = userAccountService.getUserInfo(username);
			modelMap.addAttribute("type", 2);
			applyModel(modelMap, userInfo);
			break;
		case 3:
			schools = userAccountService.getUserSchools(username);
			companies = userAccountService.getUserCompanies(username);
			modelMap.addAttribute("schools", schools);
			modelMap.addAttribute("companies", companies);
			modelMap.addAttribute("cities", cities);
			modelMap.addAttribute("type", 3);
			break;
		case 4:
			userInfo = userAccountService.getUserInfo(username);
			modelMap.addAttribute("type", 4);
			modelMap.addAttribute("cities", cities);
			applyModel(modelMap, userInfo);
			break;
		case 5:
			modelMap.addAttribute("type", 5);
			Map<String, Map<String, String>> hobbies = userAccountService.getUserHobbies(username);
			Map<String, String> categories = userAccountService.getUserCategories(username);
			modelMap.addAttribute("hobbies", hobbies);
			modelMap.addAttribute("categories", categories);
			break;
		}
		
		return getViewPrefix() + "ajaxUserAccountSettingsPart";
	}
}
